<template>
  <div>
    <el-select v-if="roles.indexOf('admin') != -1" v-model="value" placeholder="请选择城市" filterable @change="cityChange">
      <el-option
        v-for="item in city"
        :key="item.id"
        :label="item.value"
        :value="item.id">
      </el-option>
    </el-select>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { GetCityList } from '../../api/select'
export default {
  name: 'city',
  data () {
    return {
      city: [{ id: 0, value: '全部' }],
      value: ''
    }
  },
  mounted () {
    this.$store.dispatch('SetCityId', 0)
    GetCityList().then(res => {
      if (res.data.city !== null) {
        this.city = [{ id: 0, value: '全部' }].concat(res.data.city)
      } else {
        this.city = [{ id: 0, value: '全部' }]
      }
    })
  },
  methods: {
    cityChange (selVal) {
      this.$store.dispatch('SetCityId', selVal)
    }
  },
  computed: {
    ...mapGetters([
      'roles',
      'provinceId'
    ])
  },
  watch: {
    provinceId: function (value) {
      GetCityList().then(res => {
        if (res.data.city !== null) {
          this.city = [{ id: 0, value: '全部' }].concat(res.data.city)
        } else {
          this.city = [{ id: 0, value: '全部' }]
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
